package it.eng.area118.mdocommon.android.log;

import it.eng.area118.mdocommon.android.util.StringUtils;
import it.eng.area118.mdocommon.log.DailyFileLogger;
import android.os.Environment;
import android.util.Log;

public class BasicLogger implements it.eng.area118.mdocommon.log.Log {

	public static final String LOG_ENABLED = " it.eng.area118.mdocommon.android.log.BasicLogger.LOG_ENABLED";

	public static final String PATH = Environment
			.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
			.getAbsolutePath();

	private static DailyFileLogger logger;

	private static String DEFAULT_LEVEL = it.eng.area118.mdocommon.log.Log.DEBUG;

	private String level = null;

	private static BasicLogger INSTANCE;

	private static String filename;

	protected static synchronized DailyFileLogger getInstance(String level) {
		if (logger == null) {
			if (StringUtils.isEmpty(filename)) {
				filename = "emsmobile";
			}
			logger = new DailyFileLogger(PATH + "/" + filename, level, 7);
			logger.start();
		}
		return logger;
	}

	public static synchronized BasicLogger getLog() {
		if (INSTANCE == null) {
			INSTANCE = new BasicLogger();
		}
		return INSTANCE;
	}

	public static void writeLog(String tag, String text) {
		getInstance(it.eng.area118.mdocommon.log.Log.DEBUG).debug(
				tag + " - " + text);
		// Log.d(tag, text);
		// Boolean enabled = true;
		// try {
		// enabled = (Boolean) AndroidApplicationContext.getInstance()
		// .getApplicationContextValue(LOG_ENABLED);
		// } catch (Exception e) {
		// enabled = true;
		// }
		//
		// if (enabled != null && !enabled) {
		// return;
		// }
		// SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
		// SimpleDateFormat tStampFormat = new SimpleDateFormat("HH:mm:ss");
		//
		// FileWriter writer;
		// try {
		// String application = "";
		// if (AndroidApplicationContext.getInstance()
		// .getApplicationContextValue("APP_NAME") != null) {
		// application = (String) AndroidApplicationContext.getInstance()
		// .getApplicationContextValue("APP_NAME");
		// } else {
		// application = AndroidApplicationContext.getInstance()
		// .getApplicationContext().getPackageName();
		// }
		//
		// writer = new FileWriter(PATH + "/" + application + "_"
		// + formatter.format(Calendar.getInstance().getTime())
		// + ".log", true);
		// writer.write(tStampFormat.format(Calendar.getInstance().getTime())
		// + " - " + text + "\n");
		// writer.close();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

	}

	public static String getCurrentFileName() {
		return getInstance(it.eng.area118.mdocommon.log.Log.DEBUG)
				.getCompleteFileName();
		// SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
		// String application = "";
		// if
		// (AndroidApplicationContext.getInstance().getApplicationContextValue(
		// "APP_NAME") != null) {
		// application = (String) AndroidApplicationContext.getInstance()
		// .getApplicationContextValue("APP_NAME");
		// } else {
		// application = AndroidApplicationContext.getInstance()
		// .getApplicationContext().getPackageName();
		// }
		// return PATH + "/" + application + "_"
		// + formatter.format(Calendar.getInstance().getTime()) + ".log";

	}

	public static void writeLog(String tag, String text, Exception e) {
		getInstance(it.eng.area118.mdocommon.log.Log.DEBUG).error(
				tag + " - " + text, e);
		// Boolean enabled = true;
		// try {
		// enabled = (Boolean) AndroidApplicationContext.getInstance()
		// .getApplicationContextValue(LOG_ENABLED);
		// } catch (Exception e) {
		// enabled = true;
		// }
		//
		// if (enabled != null && !enabled) {
		// return;
		// }
		// try {
		// String application;
		// SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
		// SimpleDateFormat tStampFormat = new SimpleDateFormat("HH:mm:ss");
		// if (AndroidApplicationContext.getInstance()
		// .getApplicationContextValue("APP_NAME") != null) {
		// application = (String) AndroidApplicationContext.getInstance()
		// .getApplicationContextValue("APP_NAME");
		// } else {
		// application = AndroidApplicationContext.getInstance()
		// .getApplicationContext().getPackageName();
		// }
		// Log.e(tag, text + " " + e1.getMessage(), e1);
		// FileWriter writer;
		//
		// writer = new FileWriter(PATH + "/" + application + "_"
		// + formatter.format(Calendar.getInstance().getTime())
		// + ".log", true);
		// writer.write(tStampFormat.format(Calendar.getInstance().getTime())
		// + " - " + text + " " + e1.getMessage() + "\n");
		//
		// writer.close();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

	}

	public static void writeLog(String tag, String text, Throwable t) {
		getInstance(DEFAULT_LEVEL).error(tag + " - " + text, new Exception(t));
		// Boolean enabled = true;
		// try {
		// enabled = (Boolean) AndroidApplicationContext.getInstance()
		// .getApplicationContextValue(LOG_ENABLED);
		// } catch (Exception e) {
		// enabled = true;
		// }
		//
		// if (enabled != null && !enabled) {
		// return;
		// }
		// Log.d(tag, text);
		// FileWriter writer;
		// try {
		// writer = new FileWriter(PATH, true);
		// writer.write(text + " " + t.getMessage());
		// writer.close();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

	}

	@Override
	public void info(String msg) {
		Log.d("", msg);
		getInstance(level).info(msg);

	}

	@Override
	public void debug(String msg) {
		Log.d("", msg);
		getInstance(level).debug(msg);

	}

	@Override
	public void warn(String msg, Exception e) {
		Log.d("", msg, e);
		getInstance(level).warn(msg, e);

	}

	@Override
	public void warn(String msg) {
		Log.d("", msg);
		getInstance(level).warn(msg);

	}

	@Override
	public void error(String msg, Exception e) {
		Log.d("", msg, e);
		getInstance(level).error(msg, e);

	}

	@Override
	public void error(String msg) {
		Log.d("", msg);
		getInstance(msg).error(msg);

	}

	@Override
	public void start() {
		getInstance(level).start();
	}

	@Override
	public void close() {
		getInstance(level).close();

	}

	@Override
	public void reset() {
		getInstance(level).reset();

	}

	@Override
	public void setLevel(String level) {
		this.level = level;
		BasicLogger.logger.setLevel(level);
		BasicLogger.logger.reset();
	}

}
